package com.simon.hse.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.simon.hse.R;
import com.simon.hse.utils.BitmapUtils;
import com.simon.hse.utils.ToastUtils;
import com.simon.hse.view.SignView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 手写签名
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_sign)
public class SignActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.sv_sign)
    SignView mSv_sign;

    private List<Bitmap> mListBitmap = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("签名输入框");
    }


    @Event(value = {R.id.iv_common_left})
    private void finish(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;

        }
    }

    @Event(value = {R.id.tv_sign_reset, R.id.tv_sign_add, R.id.tv_sign_submit})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.tv_sign_reset:
                mSv_sign.clear();
                break;
            case R.id.tv_sign_add:
                mListBitmap.add(mSv_sign.getBitmap());
                mSv_sign.clear();
                break;
            case R.id.tv_sign_submit:
                Bitmap bitmap = mSv_sign.getBitmap();
                if (bitmap == null) {
                    ToastUtils.getInstance().showShortToast("请手写签名!");
                    return;
                }
                mListBitmap.add(bitmap);

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putInt("BITMAP_COUNT", mListBitmap.size());
                for (int i = 0; i < mListBitmap.size(); i++) {
                    String bitmapStr = BitmapUtils.convertBitmapToString(mListBitmap.get(i));
                    bundle.putString("BITMAP" + i, bitmapStr);
                }
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finishActivity(SignActivity.this);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finishActivity(SignActivity.this);
        super.onBackPressed();
    }
}
